package com.github.itdachen.utils;

import com.github.itdachen.constants.FieldAttrEnum;
import com.github.itdachen.constants.HtmlTypeEnum;
import com.github.itdachen.constants.JavaFieldConstants;
import com.github.itdachen.entity.DevTableColumn;
import com.github.itdachen.framework.core.utils.StringUtils;

import java.util.Objects;

/**
 * 字段设置
 *
 * @author 王大宸
 * @date 2025/3/11 21:09
 */
public class TableFieldUtils {


    public static void setTableField(DevTableColumn column) {
        Integer columnLength = getColumnLength(column.getColumnType());
        column.setJavaType(JavaFieldConstants.STRING);
        column.setJavaTypeTitle(JavaFieldConstants.STRING);
        column.setHtmlType(HtmlTypeEnum.INPUT.value());
        column.setHtmlTitle(HtmlTypeEnum.INPUT.text());

        /* 文本域 */
        if (columnLength >= 500 || hasElement(JavaFieldConstants.TEXT_AREA_FILED, column.getJavaField())) {
            column.setHtmlType(HtmlTypeEnum.TEXT_AREA.value());
            column.setHtmlTitle(HtmlTypeEnum.TEXT_AREA.text());
            return;
        }

        /* 图片上传 */
        if (containsElement(JavaFieldConstants.IMAGE_UPLOAD_FIELD, column.getColumnName())) {
            column.setHtmlType(HtmlTypeEnum.IMAGE_UPLOAD.value());
            column.setHtmlTitle(HtmlTypeEnum.IMAGE_UPLOAD.text());
            return;
        }

        /* 单选框 */
        if (containsElement(JavaFieldConstants.RADIO_FILED, column.getColumnName())) {
            column.setHtmlType(HtmlTypeEnum.RADIO.value());
            column.setHtmlTitle(HtmlTypeEnum.RADIO.text());
            return;
        }

        FieldAttrEnum fieldAttr = getFieldAttr(column);
        column.setJavaType(fieldAttr.javaType());
        column.setJavaTypeTitle(fieldAttr.javaType());
        column.setHtmlType(fieldAttr.htmlType().value());
        column.setHtmlTitle(fieldAttr.htmlType().text());

    }


    /***
     * 内置数据库类型
     *
     * @author 王大宸
     * @date 2025/3/11 21:33
     * @param column column
     * @return com.github.itdachen.constants.FieldAttrEnum
     */
    public static FieldAttrEnum getFieldAttr(DevTableColumn column) {
        boolean b = hasElement(FieldAttrEnum.LONG.attr(), column.getColumnType());
        if (b) {
            return FieldAttrEnum.LONG;
        }

        b = hasElement(FieldAttrEnum.INTEGER.attr(), column.getColumnType());
        if (b) {
            return FieldAttrEnum.INTEGER;
        }

        b = hasElement(FieldAttrEnum.DOUBLE.attr(), column.getColumnType());
        if (b) {
            return FieldAttrEnum.DOUBLE;
        }
        b = hasElement(FieldAttrEnum.FLOAT.attr(), column.getColumnType());
        if (b) {
            return FieldAttrEnum.FLOAT;
        }
        b = hasElement(FieldAttrEnum.BIG_DECIMAL.attr(), column.getColumnType());
        if (b) {
            return FieldAttrEnum.BIG_DECIMAL;
        }
        b = hasElement(FieldAttrEnum.BOOLEAN.attr(), column.getColumnType());
        if (b) {
            return FieldAttrEnum.BOOLEAN;
        }
        b = hasElement(FieldAttrEnum.TEXT_AREA.attr(), column.getColumnType());
        if (b) {
            return FieldAttrEnum.TEXT_AREA;
        }
        b = hasElement(FieldAttrEnum.LOCAL_DATE_TIME.attr(), column.getColumnType());
        if (b) {
            return FieldAttrEnum.LOCAL_DATE_TIME;
        }

        return FieldAttrEnum.STRING;
    }


    /***
     * 数组中是否包含某个元素
     *
     * @author 王大宸
     * @date 2025/3/11 21:13
     * @param array array
     * @param target target
     * @return boolean
     */
    public static boolean hasElement(String[] array, String target) {
        for (String i : array) {
            if (Objects.equals(i.toLowerCase(), target.toLowerCase())) {
                return true;
            }
        }
        return false;
    }

    /***
     * 是否包含
     *
     * @author 王大宸
     * @date 2025/3/11 21:26
     * @param array array
     * @param target target
     * @return boolean
     */
    public static boolean containsElement(String[] array, String target) {
        for (String i : array) {
            if (target.toLowerCase().contains(i.toLowerCase())) {
                return true;
            }
        }
        return false;
    }


    /***
     * 获取字段长度
     *
     * @author 王大宸
     * @date 2022/8/14 16:04
     * @param columnType columnType
     * @return java.lang.Integer
     */
    private static Integer getColumnLength(String columnType) {
        if (StringUtils.indexOf(columnType, "(") > 0) {
            String length = StringUtils.substringBetween(columnType, "(", ")");
            return Integer.valueOf(length);
        } else {
            return 0;
        }
    }

    private static String getDbType(String columnType) {
        if (StringUtils.indexOf(columnType, "(") > 0) {
            return StringUtils.substringBefore(columnType, "(");
        } else {
            return columnType;
        }
    }


}
